package cn.yusei.teamTribe.service.team;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.yusei.teamTribe.constant.PermissionType;
import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.dao.team.TeamAlbumDAO;
import cn.yusei.teamTribe.dao.team.TeamPhotoDAO;
import cn.yusei.teamTribe.dao.team.po.TeamPhotoPO;
import cn.yusei.teamTribe.service.PermissionService;

@Service
public class TeamPhotoService {

	@Autowired
	private TeamPhotoDAO teamPhotoDAO;
	@Autowired
	private TeamAlbumDAO tpaDAO;
	@Autowired
	private PermissionService permissionService;
	
	public int add(int teamId, int albumId, int userId, String fileId)
	{
		if(!permissionService.havePermission(userId, teamId, PermissionType.UPDATE_TEAM_INFO, 0))
			return ResponseCode.PERMISSION_FORBIDDEN;
		if(!tpaDAO.exists(albumId, teamId))
			return ResponseCode.ALBUM_NOT_EXISTS;
		TeamPhotoPO teamPhotoPO = new TeamPhotoPO();
		teamPhotoPO.setTeamId(teamId);
		teamPhotoPO.setAlbumId(albumId);
		teamPhotoPO.setUserId(userId);
		teamPhotoPO.setFileId(fileId);
		teamPhotoDAO.insert(teamPhotoPO);
		return ResponseCode.REQUEST_SUCCESSED;
	}
	
	public int delete(int teamId, int albumId, int userId, String fileId)
	{
		if(!permissionService.havePermission(userId, teamId, PermissionType.UPDATE_TEAM_INFO, 0))
			return ResponseCode.PERMISSION_FORBIDDEN;
		if(!tpaDAO.exists(albumId, teamId))
			return ResponseCode.ALBUM_NOT_EXISTS;
		TeamPhotoPO teamPhotoPO = new TeamPhotoPO();
		teamPhotoPO.setTeamId(teamId);
		teamPhotoPO.setAlbumId(albumId);
		teamPhotoPO.setUserId(userId);
		teamPhotoPO.setFileId(fileId);
		teamPhotoDAO.delete(teamPhotoPO);
		return ResponseCode.REQUEST_SUCCESSED;
	}
	
	public String[] get(int teamId, int albumId, int offset, int num)
	{
		String[] files = null;
		files = teamPhotoDAO.select(teamId, albumId, offset, num);
		return files;
	}
}
